﻿Imports NativeWifi
Imports NativeWifi.WlanClient
Imports NativeWifi.Wlan

Module WI_FI_Netwoks
    Public Function get_avaible_netwoks()
        Dim client As NativeWifi.WlanClient = New NativeWifi.WlanClient
        Dim avaibleSSID() As String
        Dim tSSID As String = ""
        Dim i As Int16 = 0
        Dim trovato As String = ""
        For Each wlanIface As NativeWifi.WlanClient.WlanInterface In client.Interfaces
            ' Lists all available networks
            'Dim networks() As Wlan.WlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0)
            For Each network As Wlan.WlanAvailableNetwork In wlanIface.GetAvailableNetworkList(0)
                ReDim Preserve avaibleSSID(i)
                tSSID = GetStringForSSID(network.dot11Ssid)
                For Each SSID As String In avaibleSSID
                    If SSID = tSSID Then
                        trovato = "si"
                        Exit For
                    End If
                Next
                If trovato = "si" Then
                    trovato = ""
                Else
                    avaibleSSID(i) = tSSID
                    i += 1
                End If
            Next
        Next
        Array.Sort(avaibleSSID)
        Return avaibleSSID
        ReDim avaibleSSID(0)
        'For i2 As Integer = 0 To avaibleSSID.GetUpperBound(0)
        'avaibleSSID(i2) = ""
        'Next
    End Function

    Public Function GetStringForSSID(ByVal ssid As Wlan.Dot11Ssid) As String
        Return System.Text.Encoding.ASCII.GetString(ssid.SSID, 0, CType(ssid.SSIDLength, Integer))
    End Function

    Public Function get_mac_address(ByVal ssid As String) As String
        Dim wlanClient = New WlanClient()
        Dim mac As String = ""

        For Each wlanInterface As WlanClient.WlanInterface In wlanClient.Interfaces
            For Each wlanBssEntry As Wlan.WlanBssEntry In wlanInterface.GetNetworkBssList()

                If ssid = GetStringForSSID(wlanBssEntry.dot11Ssid) Then
                    Dim tmacAddr As Byte() = wlanBssEntry.dot11Bssid
                    For i As Byte = 0 To tmacAddr.Length - 1
                        mac &= (tmacAddr(i).ToString("x2"))
                    Next
                    Exit For
                End If
            Next
        Next

        Return mac
    End Function
    Public Function get_network_info(ByVal ssid As String)
        Try
            Dim wlanClient = New WlanClient()
            Dim client As NativeWifi.WlanClient = New NativeWifi.WlanClient
            Dim mac As String = ""
            For Each wlanIface As NativeWifi.WlanClient.WlanInterface In client.Interfaces
                ' Lists all available networks
                For Each network As Wlan.WlanAvailableNetwork In wlanIface.GetAvailableNetworkList(0)
                    For Each wlanBssEntry As Wlan.WlanBssEntry In wlanIface.GetNetworkBssList()

                        If ssid = GetStringForSSID(network.dot11Ssid) Then
                            Dim tmacAddr As Byte() = wlanBssEntry.dot11Bssid
                            For i As Byte = 0 To tmacAddr.Length - 1
                                mac &= (tmacAddr(i).ToString("x2"))
                            Next
                            Dim net_info() As String = {GetStringForSSID(network.dot11Ssid), network.dot11BssType, network.dot11DefaultAuthAlgorithm, network.dot11DefaultCipherAlgorithm, network.wlanSignalQuality, mac}
                            Return net_info
                            Exit For
                        End If

                    Next
                Next
            Next
        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            ProjectData.ClearProjectError()
        End Try
    End Function
    Public Function get_network_info()
        Dim wlanClient = New WlanClient()
        Dim client As NativeWifi.WlanClient = New NativeWifi.WlanClient
        Dim mac As String = ""
        For Each wlanIface As NativeWifi.WlanClient.WlanInterface In client.Interfaces
            ' Lists all available networks
            Dim network() As Wlan.WlanAvailableNetwork = wlanIface.GetAvailableNetworkList(0)
            Dim wlanBssEntry() As Wlan.WlanBssEntry = wlanIface.GetNetworkBssList()

        Next

    End Function
End Module
